<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>分配角色</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="apple-mobile-web-app-status-bar-style" content="black">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="format-detection" content="telephone=no">
	<link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all" />
</head>
<body class="childrenBody">
	<form class="layui-form" id="boxRoleForm">
        <ul id="treeDemo" class="ztree"></ul>
		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit="" lay-filter="editResources">立即提交</button>
		    </div>
		</div>
	</form>
	<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
	<script type="text/javascript" src="/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/js/jquery.ztree.excheck.js"></script>
	<script type="text/javascript" src="/plugins/layui/layui.js"></script>
<!-- 	<script type="text/javascript" src="/js/user/newsAdd.js"></script> -->
	
	<script type="text/javascript">
	layui.config({
		 base: '/js/admin/'
	}).use(['form','layer','jquery','layedit','laydate','$tool'],function(){
		var form = layui.form,
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			laypage = layui.laypage,
			layedit = layui.layedit,
			laydate = layui.laydate,
			$ = layui.jquery;
		 $tool = layui.$tool;
		
		 	form.on("submit(editResources)",function(data){
		 		  var queryArgs = $tool.getQueryParam();//获取查询参数
		 	        var id = queryArgs['id'];
		 	       saveRoleResources(id);
		 	})
	     initUserInfo();
	     /**
	      * 初始化用户信息
	      * */
	      function initUserInfo() {
	         var queryArgs = $tool.getQueryParam();//获取查询参数
	         var rid = queryArgs['id'];
	         roleId = rid;
		        var setting = {
		            check: {
		                enable: true,
		                chkboxType:  { "Y" : "p", "N" : "s" }
		            },
		            data: {
		                simpleData: {
		                    enable: true,
		                    idKey: "id",
		                    pIdKey: "parentid",
		                }
		            }
		        };

		        $.ajax({
		            async:false,
		            type : "POST",
		            data:{rid:rid},
		            url: "resources/resourcesWithSelected",
		            dataType:'json',
		            success: function(data){

		                $.fn.zTree.init($("#treeDemo"), setting, data);
		                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		                zTree.expandAll(true);
		                
		                form.render();
		               // $('#selectResources').modal();
		            }
		        });
	     }
	     
	      //保存权限的选择
	      function saveRoleResources(roleId) {
	          var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
	                  checkNode= zTree.getCheckedNodes(true);
	          var ids = new Array();
	          for(var i=0;i<checkNode.length;i++){
	              ids.push(checkNode[i].id);
	          }
	          $.ajax({
	              async:false,
	              type : "POST",
	              data:{roleid:roleId,resourcesid:ids.join(",")},
	              url: "roles/saveRoleResources",
	              success: function(data){
	                  if(data=="success"){
	                      layer.msg('保存成功');
	                      $('#selectResources').modal('hide');
	                  }else{
	                      layer.msg('保存失败');
	                      $('#selectResources').modal('hide');
	                  }
	              }
	          });
	      }
		
		//创建一个编辑器
	 	form.on("submit(addWelfares)",function(data){
	 		var datas=data.field;
	 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
	 		$.ajax({  
	            type: "POST",  
	            url:"/welfares",  
	            data:JSON.stringify(datas),// 序列化表单值  
	         //   dataType: "json",
	            contentType:"application/json",
	           
	            success: function(res) {  
	            	if(res.code==2000){
	            		top.layer.close(index);
	          			top.layer.msg("添加成功！");
	           			layer.closeAll("iframe");
	          	 		//刷新父页面
	          	 		parent.location.reload();
	            	}
	            
	            } ,
		 		 error: function(request) {  
		             alert("Connection error");  
		         } 
	            
	            
	        }); 
	 		return false;
	 	})
		
	})
	</script>
</body>
</html>

